package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.AddrCreateParams;
import com.huizhonglm.diap.api.model.user.AddrDefaultParams;
import com.huizhonglm.diap.api.model.user.AddrDeleteParams;
import com.huizhonglm.diap.api.model.user.AddrUpdateParams;
import com.huizhonglm.diap.ui.user.iView.AddrMgrIView;

/**
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-19 11:25
 */
public class AddrMgrPresenter {

    private AddrMgrIView mView;

    public AddrMgrPresenter(AddrMgrIView view) {
        mView = view;
    }

    public void createAddr(AddrCreateParams params) {
        API.http().post("createAddress", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        mView.createSuccess();
                    } else {
                        mView.createFailure();
                    }
                } else {
                    mView.createFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.createFailure();
            }
        });
    }

    public void updateAddr(AddrUpdateParams params) {
        API.http().post("updateAddress", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        mView.updateSuccess();
                    } else {
                        mView.updateFailure();
                    }
                } else {
                    mView.updateFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.updateFailure();
            }
        });
    }

    public void deleteAddr(String addrId) {
        AddrDeleteParams params = new AddrDeleteParams();
        params.addrId = addrId;
        API.http().post("deleteAddress", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        mView.deleteSuccess();
                    } else {
                        mView.deleteFailure();
                    }
                } else {
                    mView.deleteFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.deleteFailure();
            }
        });
    }

    public void setDefault(String addrId, String userId) {
        AddrDefaultParams params = new AddrDefaultParams();
        params.addrId = addrId;
        params.userId = userId;
        API.http().post("defaultAddress", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse res = JSON.parseObject(result, CommonResponse.class);
                    if (res != null && Status.SUCCESS.equals(res.getStatus())) {
                        mView.defaultSuccess();
                    } else {
                        mView.defaultFailure();
                    }
                } else {
                    mView.defaultFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.defaultFailure();
            }
        });
    }
}
